$(document).ready(function(){
	$.fn.filterParent = function(filter) {
		var obj = $(this);
		while(obj.tagName != "html") {
			obj = obj.parent(filter);
			if(obj == null) {
				obj = obj.parent();
			}
			else {
				break;
			}
			alert(obj.attr("class"));
		}
		if(obj.tagName == "html") {
			obj = null;
		}
		return obj;
	};
	
	$("input[data-itemtype='test']").click(function(){
		var ret = $(this).closest("form").attr("action");
		var ss = (ret == null);
		alert(ss);
		//alert($(this).attr("tagName"));
		//alert(this.tagName);
		//$(this).filterParent("form");
	});
	
	/* reset the prompt to blank once grap focus */
	$("input[data-validate-type]").focus(function(){
		var inputId = $(this).attr("id");
		var tmp = inputId.split("_");
		var formName = tmp[0];
		var inputName = tmp[1];
		var ctrlgrp = $("#"+formName+"_"+inputName+"_ctrlgrp");
		var helpspan = $("#"+formName+"_"+inputName+"_help");
		ctrlgrp.removeClass("error");
		helpspan.html("");
	});
	/* do validate when blur */
	$("input[data-validate-type]").blur(function(){
		var inputId = $(this).attr("id");
		var tmp = inputId.split("_");
		var formName = tmp[0];
		var inputName = tmp[1];
		var formObj = $("#"+formName);
		var ctrlgrp = $("#"+formName+"_"+inputName+"_ctrlgrp");
		var helpspan = $("#"+formName+"_"+inputName+"_help");
		var lblName = $("#"+formName+"_"+inputName+"_lbl").text();
		
		/*required - cant be null*/
		if($(this).filter("[data-validate-type*='required']") != null) {
			var value = $(this).val();
			if(value == null || value == "") {
				ctrlgrp.addClass("error");
				helpspan.html(lblName+"不能为空");
				return;
			}
		}
		/*ajax - need to verify*/
		if($(this).filter("[data-validate-type*='ajax']") != null) {
			var value = $(this).val();
			var errflag = false;
			$.post(
				formObj.attr("action"),
				{
					"ajax":formObj.attr("id"),
					"value":value
				},
				function(json) {
					if(json.code > 0) {
						ctrlgrp.addClass("error");
						helpspan.html(json.message);
						errflag = true;
					}
					else {
						errflag = false;
					}
				},
				"json"
			);
			if(errflag) {
				return;
			}
		}
	});
});